using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
  public  class tbCTDonHangDAL
     {
        private SqlConnection cn = null;
        public tbCTDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbCTDonHangDAL(tbCTDonHangDAL ctdonhang)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbCTDonHang ctdonhang)
        {
            SqlCommand cm = new SqlCommand("sp_tbCTDonHang_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaDH", ctdonhang.MaDH);
            cm.Parameters.AddWithValue("@SanPham", ctdonhang.SanPham);
            cm.Parameters.AddWithValue("@SoLuong", ctdonhang.SoLuong);
            cm.Parameters.AddWithValue("@DonGia", ctdonhang.DonGia);
            cm.ExecuteNonQuery();
        }
      public void Update(tbCTDonHang ctdonhang)
        {
            SqlCommand cm = new SqlCommand("sp_tbCTDonHang_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaCTDH", ctdonhang.MaCTDH);
            cm.Parameters.AddWithValue("@MaDH", ctdonhang.MaDH);
            cm.Parameters.AddWithValue("@SanPham", ctdonhang.SanPham);
            cm.Parameters.AddWithValue("@SoLuong", ctdonhang.SoLuong);
            cm.Parameters.AddWithValue("@DonGia", ctdonhang.DonGia);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbCTDonHang_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaCTDH", iD);
            cm.ExecuteNonQuery();
        }
        public tbCTDonHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbCTDonHang_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbCTDonHangCollection ctdhCollection = new tbCTDonHangCollection();

            while (dr.Read())
            {
                tbCTDonHang ctdonhang = new tbCTDonHang();
                ctdonhang.MaCTDH = int.Parse(dr.GetValue(0).ToString());
                ctdonhang.MaDH = int.Parse(dr.GetValue(1).ToString());
                ctdonhang.SanPham = int.Parse(dr.GetValue(2).ToString());
                ctdonhang.SoLuong = int.Parse(dr.GetValue(3).ToString());
                ctdonhang.DonGia = int.Parse(dr.GetValue(4).ToString());
                ctdhCollection.Add(ctdonhang);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctdhCollection;
        }
    }
}


